package com.bicon.botu.mall.component.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.component.common.PageContentConstants;
import com.bicon.botu.mall.component.entity.PageContent;
import com.bicon.botu.mall.component.service.IPageContentService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典Controller
 *
 * @author 张露晨
 * @since 2017-07-08
 */
@RestController
@RequestMapping("/V1.0.0/pageContext")
public class PageContentController extends BaseController{

    @Autowired
    private IPageContentService iPageContentService;

    @Autowired
    private RedisHandler redisHandler;

    /**
     * 初始化单页到缓存
     *
     * @return
     */
    @RequestMapping(value = "/initPageContentToRedis", method = RequestMethod.POST)
    public BaseResult initPageContentToRedis() {
        List<PageContent> pageContentList = iPageContentService.selectList(new EntityWrapper<PageContent>().eq("logic_delete", PageContentConstants.DEL_LOGIC_NORMAL));
        Map<String, String> map = new HashMap<>(16);
        if (CollectionUtils.isNotEmpty(pageContentList)) {
            pageContentList.stream().forEach(pageContent -> map.put(pageContent.getDictionaryCode(), pageContent.getContent()));
            redisHandler.getGlobalRedisTemplate().cacheHashAll(PageContentConstants.PAGE_REDIS_KEY,"",map);
        }
        return sendResult200(map);
    }


    /**
     * （批量）新增字典内容
     *
     * @param dictionaryCode 字典Code
     * @param title          标题
     * @param author         作者
     * @param content        内容
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult addPageContent(@RequestParam(value = "dictionaryCode") String dictionaryCode,
                                     @RequestParam(value = "title") String title,
                                     @RequestParam(value = "author") String author,
                                     @RequestParam(value = "content") String content) {
        return iPageContentService.addPageContent(dictionaryCode, title, author, content);
    }

    /**
     * （批量）逻辑删除字典内容
     *
     * @param idJson 主键
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public BaseResult del(@RequestParam(value = "idJson") String idJson) {
        return iPageContentService.del(idJson);
    }

    /**
     * 修改字典内容
     *
     * @param id             主键
     * @param dictionaryCode 字典Code
     * @param title          标题
     * @param author         作者
     * @param content        内容
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam(value = "id") String id,
                             @RequestParam(value = "dictionaryCode", required = false) String dictionaryCode,
                             @RequestParam(value = "title", required = false) String title,
                             @RequestParam(value = "author", required = false) String author,
                             @RequestParam(value = "content", required = false) String content) {
        return iPageContentService.update(id, dictionaryCode, title, author, content);
    }

    /**
     * 根据字典code查找字典
     *
     * @param dictionaryCode 字典Code
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public BaseResult select(@RequestParam(value = "dictionaryCode") String dictionaryCode) {
        return iPageContentService.select(dictionaryCode);
    }

    /**
     * 根据字典code查找字典
     *
     * @param dictionaryCode 字典Code
     * @return
     */
    @RequestMapping(value = "/static/select", method = RequestMethod.POST)
    public BaseResult selectSharePage(@RequestParam(value = "dictionaryCode") String dictionaryCode) {
        return iPageContentService.select(dictionaryCode);
    }

    /**
     * 根据字典code分页查找字典
     *
     * @param pageNo         页码
     * @param pageSize       每页记录数
     * @param dictionaryCode 字典Code
     * @return
     */
    @RequestMapping(value = "/selectPage", method = RequestMethod.POST)
    public BaseResult selectPage(@RequestParam(value = "pageNo") String pageNo,
                                 @RequestParam(value = "pageSize") String pageSize,
                                 String dictionaryCode) {
        return iPageContentService.selectPage(pageNo, pageSize, dictionaryCode);
    }

}
